crypto: prevent Hmac.digest() from returning uninitialized memory after stream use - #65112
crypto: prevent Hmac.digest() from returning uninitialized memory after stream use#65112mcollina wants to merge 1 commit into
Conversation
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65112 +/- ##
==========================================
- Coverage 92.01% 90.13% -1.88%
==========================================
Files 379 751 +372
Lines 166972 252304 +85332
Branches 25554 47442 +21888
==========================================
+ Hits 153639 227425 +73786
- Misses 13041 16197 +3156
- Partials 292 8682 +8390
🚀 New features to boost your workflow:
|
e410836 to
e41218a
Compare
There was a problem hiding this comment.
Can you add a comment explaining why the classes can no longer share that method? I've tried to come up with one, according to the LLM I'm using it's related to DEP0206 (which makes me wonder: is that change backportable?)
| Hmac.prototype._flush = function _flush(callback) { | |
| this.push(this[kHandle].digest()); | |
| this[kState][kFinalized] = true; // This diverges from Hash.prototype._flush: | |
| // Hash instances are still usable after a flush, Hmac are not, see DEP0206. | |
| callback(); | |
| }; |
There was a problem hiding this comment.
Done — added the comment explaining that this diverges from Hash.prototype._flush (Hash instances are usable after a flush, Hmac are not, per DEP0206).
There was a problem hiding this comment.
nit
| assert.deepStrictEqual(streamHmac.digest(), Buffer.alloc(0)); |
There was a problem hiding this comment.
Done — switched the assertion to Buffer.alloc(0) as suggested.
Hmac.prototype._flush was aliased to Hash.prototype._flush, which finalizes the native HMAC context but never sets the JavaScript-side kFinalized flag. After an Hmac has been used as a stream, a subsequent Hmac.prototype.digest() call therefore still believes the object has not been finalized and calls into C++ a second time. On that second call the native context has already been reset, so the digest buffer is never written and Digest::MAX_SIZE bytes of uninitialized stack memory are returned to JavaScript. Hash is not affected because Hash::HashDigest caches its digest (refs nodejs#28245); Hmac never received the equivalent protection. Give Hmac its own _flush that sets kFinalized so repeat digest() calls after stream use are handled by the existing DEP0206 guard. As defense in depth, also set buf.len = 0 on the native side when the context has already been reset so unwritten bytes can never be emitted. Signed-off-by: Matteo Collina <hello@matteocollina.com>
e41218a to
49e88ce
Compare
Hmac.prototype._flushwas aliased toHash.prototype._flush, which finalizes the native HMAC context but never sets the JavaScript-sidekFinalizedflag. After anHmachas been used as a stream, a subsequentHmac.prototype.digest()call therefore still believes the object has not been finalized and calls into C++ a second time. On that second call the native context has already been reset, so the digest buffer is never written andDigest::MAX_SIZEbytes of uninitialized stack memory are returned to JavaScript.Hashis not affected becauseHash::HashDigestcaches its digest (refs #28245);Hmacnever received the equivalent protection.This gives
Hmacits own_flushthat setskFinalized, so repeatdigest()calls after stream use are handled by the existing DEP0206 guard. As defense in depth, it also setsbuf.len = 0on the native side when the context has already been reset, so unwritten bytes can never be emitted. A regression test covers the stream-then-digest()path.